﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLTuyenXe.DAL;
using System.Collections;
using System.Data;
using QLTuyenXe.DAO;
using System.Windows.Forms;

namespace QLTuyenXe.BLL
{
    class BDieuHanh
    {
        MyConnectUnit myConnU = new MyConnectUnit();

        public bool kt(string condition)
        {
            bool kt = false;
            ArrayList dieuhanhs = new ArrayList();
            DataTable tb = new DataTable();
            tb = myConnU.Select("DIEU_HANH", "*", "maxe='" + condition + "'");
            if (tb.Rows.Count > 0)
            {
                kt = false;
            }
            else
            {
                kt = true;
            }
            return kt;
        }

        //public ArrayList Select(String columns, String condition)
        //{
        //    return this.Select(columns, condition, null);
        //}

        //public ArrayList Select(String columns)
        //{
        //    return this.Select(columns, null);
        //}

        //public ArrayList Select()
        //{
        //    return this.Select("*");
        //}

        public ArrayList Selects()
        {
            ArrayList dieuhanhs = new ArrayList();
            DataTable tb = new DataTable();

            tb = myConnU.Selects("DIEU_HANH", "QUAN_LY_XE", "TUYEN_XE", "maDH", "bienso", "tentuyen", "trangthai", "DIEU_HANH.maxe = QUAN_LY_XE.maxe", "DIEU_HANH.matuyenxe = TUYEN_XE.matuyenxe");
            if (tb != null)
                foreach (DataRow r in tb.Rows)
                {
                    DH dh = new DH();
                    dh.Id = (r["maDH"].ToString());
                    dh.Bienso = (r["bienso"].ToString());
                    dh.Tentuyen = (r["tentuyen"].ToString());
                    dh.Trangthai = (r["trangthai"].ToString());
                    dieuhanhs.Add(dh);
                }
            return dieuhanhs;
        }

        //public ArrayList Selects()
        //{
        //    return this.Selects();
        //}

        //public ArrayList Selects(String columns1, String columns2, String columns3, String columns4)
        //{
        //    return this.Selects(columns1, columns2, columns3, columns4, null, null);
        //}

        //public ArrayList Selects()
        //{
        //    return this.Selects("*");
        //}

        public String getOrder()
        {
            string strID = myConnU.GetTopDataCell("DIEU_HANH", "maDH");
            string strID1;
            string strID2;
            strID1 = strID.Substring(0, 2);
            strID2 = strID.Substring(2, 5);
            int autoNum = Convert.ToInt32(strID2) + 1;

            if (autoNum < 10)
                return strID1 + "0000" + autoNum;

            else if (autoNum >= 10 && autoNum < 100)
                return strID1 + "000" + autoNum;

            else if (autoNum >= 100 && autoNum < 1000)
                return strID1 + "00" + autoNum;
            else if (autoNum >= 1000 && autoNum < 10000)
                return strID1 + "0" + autoNum;
            else if (autoNum >= 10000 && autoNum < 100000)
                return strID1 + "" + autoNum;

            else
                return "";
        }

        public int Insert(DIEUHANH dieuhanh)
        {
            ColumnData id = new ColumnData("maDH", 1, dieuhanh.Id.ToString());
            ColumnData maxe = new ColumnData("maxe", 1, dieuhanh.Maxe.ToString());
            ColumnData tuyenxe_id = new ColumnData("matuyenxe", 1, dieuhanh.Tuyenxe_id.ToString());
            ColumnData trangthai = new ColumnData("trangthai", 1, dieuhanh.Trangthai.ToString());
            ArrayList columnData = new ArrayList();
            columnData.Add(id);
            columnData.Add(maxe);
            columnData.Add(tuyenxe_id);
            columnData.Add(trangthai);
            return myConnU.Insert("DIEU_HANH", columnData);
        }

        public int Insert(ArrayList dieuhanhs)
        {
            int count = 0;
            foreach (DIEUHANH dh in dieuhanhs)
            {
                count += this.Insert(dh);
            }
            return count;
        }

        public int Update(DIEUHANH dieuhanh, String condition)
        {
            ColumnData maxe = new ColumnData("maxe", 1, dieuhanh.Maxe.ToString());
            ColumnData tuyenxe_id = new ColumnData("matuyenxe", 1, dieuhanh.Tuyenxe_id.ToString());
            ColumnData trangthai = new ColumnData("trangthai", 1, dieuhanh.Trangthai.ToString());
            ArrayList columnDatas = new ArrayList();
            columnDatas.Add(maxe);
            columnDatas.Add(tuyenxe_id);
            columnDatas.Add(trangthai);
            return myConnU.Update("DIEU_HANH", columnDatas, condition);
        }

        public int Update(ArrayList dieuhanhs, String condition)
        {
            int count = 0;
            foreach (DIEUHANH dh in dieuhanhs)
            {
                count += this.Update(dh, condition);
            }
            return count;
        }

        public int Delete(String condition)
        {
            return myConnU.Delete("DIEU_HANH", condition);
        }

        public int GetMaxId()
        {
            return myConnU.GetMaxId("DIEU_HANH", "maDH");
        }
    }
}
